package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.domain.CheckGroup;
import com.itheima.domain.CheckItem;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;
    @Override
    public void insertCheckGroup(CheckGroup checkGroup, Integer[] checkitemIds) {
        //将新增的检查组添加到数据库
        checkGroupDao.insertCheckGroup(checkGroup);
        //设置中间表t_checkgroup_checkitem
        //获得新插入数据的id
        Integer checkGroupId = checkGroup.getId();
        insertCheckGroup(checkitemIds, checkGroupId);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();

        Integer pageSize = queryPageBean.getPageSize();

        String queryString = queryPageBean.getQueryString();
        //忽略字符串里的空格
        if (queryString != null && queryString != "") {
            queryString = queryString.replaceAll(" ","");
        }

        PageHelper.startPage(currentPage,pageSize);

        Page<CheckGroup> checkGroupPage = checkGroupDao.findCondition(queryString);

        return new PageResult(checkGroupPage.getTotal(),checkGroupPage.getResult());
    }

    @Override
    public Map<String, Object> findId(Integer id) {
        //根据id去查询检查组
        CheckGroup checkGroup = checkGroupDao.findId(id);
        //查询检查组所对应的检查项的id
        Integer[] checkitemIds = checkGroupDao.findCheckGroupAndCheckitem(id);
        //查询所有检查项
        List<CheckItem> list = checkItemDao.findAll();
        //将查询结果存入map
        Map<String, Object> map = new HashMap<>();
        map.put("checkGroup",checkGroup);
        map.put("checkitemIds",checkitemIds);
        map.put("checkItem",list);
        return map;
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //编辑检查组
        checkGroupDao.edit(checkGroup);
        //根据id删除检查组和检查项的关联
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.delCheckGroupAndCheckItem(checkGroupId);
        //重新建立检查组于检查项的关联
        insertCheckGroup(checkitemIds, checkGroupId);
    }

    private void insertCheckGroup(Integer[] checkitemIds, Integer checkGroupId) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitemId", checkitemId);
                checkGroupDao.addCheckGroupAndCheckItem(map);
            }
        }
    }

    @Override
    public List<CheckGroup> findAll() {

        return checkGroupDao.findAll();
    }

}
